﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Crosland.Automotriz.Logic.Linq;
using Crosland.Automotriz.Core;
using Crosland.Automotriz.Facade.Facade;
using Crosland.Automotriz.Facade.Interface;
using System.Configuration;

namespace Crosland.Automotriz.Web.Repuestos
{
    public partial class wFrmManualesPartesVehiculosSistemaArticulos : System.Web.UI.Page
    {
        iFacadeSistema BL_T_Man_SistemaArticulos = new FacadeSistema();
        string idNegocio = ConfigurationManager.AppSettings["Negocio"];
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ScriptManager.RegisterStartupScript(this, typeof(Page), "carga", "JavaScript:confirmSubmitResult();", true);
                listadoArticulos();
                gvlistado.FocusedRowIndex = -1;
            }
        }

        void listadoArticulos()
        {
            gvlistado.DataSource = BL_T_Man_SistemaArticulos.listar_Sistema_Articulos();
            gvlistado.DataBind();

        }

        protected void gvlistado_FocusedRowChanged(object sender, EventArgs e)
        {
            txtCodigo.Text = Convert.ToString(gvlistado.GetRowValues(gvlistado.FocusedRowIndex, "COD_MATERIAL"));
            txtDescripcion.Text = Convert.ToString(gvlistado.GetRowValues(gvlistado.FocusedRowIndex, "NOM_MATERIAL_ESP")); 
            txtDescripcionIngles.Text = Convert.ToString(gvlistado.GetRowValues(gvlistado.FocusedRowIndex, "NOM_MATERIAL_INGLES"));
           
        }

        protected void gvlistado_AfterPerformCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
        {
            listadoArticulos();
        }

        protected void btnGrabarArticulo_Click(object sender, EventArgs e)
        {
            T_MAN_ARTICULO objt_man_articulo = new T_MAN_ARTICULO();

            try
            {
                objt_man_articulo.COD_MATERIAL = txtCodigo.Text;
                objt_man_articulo.NOM_MATERIAL_ESP = txtDescripcion.Text;
                objt_man_articulo.NOM_MATERIAL_INGLES = txtDescripcionIngles.Text;
               // objt_man_articulo.DES_OBSERVACION_TECNICA = txtObservacionTecnicaModificar.Text;
                //if (txtObservacionTecnicaModificar.Text.Trim() != string.Empty) {
                //    objt_man_articulo.IND_OBSERVACION_TECNICA = 1;
                //}

                if (BL_T_Man_SistemaArticulos.Modificar_Sistema_Articulos(objt_man_articulo))
                {
                    listadoArticulos();
                    string script = "pcModificarArticulo.Hide();popupUpdate.Show();";
                    ScriptManager.RegisterStartupScript(this, GetType(), "carga", script, true);
                }
                else
                {
                    string script = "pcModificarArticulo.Hide();popupError.Show();";
                    ScriptManager.RegisterStartupScript(this, GetType(), "carga", script, true);
                }


            }
            catch (Exception)
            {

            }
        }

        protected void btnGrabarArticuloNew_Click(object sender, EventArgs e)
        {
            if (txtCodigoNew.Text == "" || txtDescripcionNew.Text == "" || txtDescripcionInglesNew.Text == "")
            {
                string script = "popupError.Show();";
                ScriptManager.RegisterStartupScript(this, typeof(Page), "carga", script, true);

            }
            else
            {

                T_MAN_ARTICULO objt_man_articulo = new T_MAN_ARTICULO();
                try
                {
                    objt_man_articulo.COD_MATERIAL = txtCodigoNew.Text;
                    objt_man_articulo.NOM_MATERIAL_ESP = txtDescripcionNew.Text;
                    objt_man_articulo.NOM_MATERIAL_INGLES = txtDescripcionInglesNew.Text;
                   // objt_man_articulo.DES_OBSERVACION_TECNICA = txtObservacionTecnicaModificar.Text;
                    //if (txtObservacionTecnicaModificar.Text.Trim() != string.Empty)
                    //{
                    //    objt_man_articulo.IND_OBSERVACION_TECNICA = 1;
                    //}

                    if (BL_T_Man_SistemaArticulos.Insertar_Sistema_Articulos(objt_man_articulo))
                    {
                        string script = "pcNuevoArticulo.Hide();popupOK.Show();";
                        ScriptManager.RegisterStartupScript(this, GetType(), "carga", script, true);
                    }
                    else
                    {
                        string script = "pcNuevoArticulo.Hide();popupErrorInsert.Show();";
                        ScriptManager.RegisterStartupScript(this, GetType(), "carga", script, true);
                    }
                }
                catch (Exception)
                {

                }
            }

        }

        protected void btnAnular_Click(object sender, EventArgs e)
        {
            int seleccion = Convert.ToInt32(gvlistado.FocusedRowIndex.ToString());
            if (seleccion != -1)
            {
                string script = "popupAnularOK.Show();";
                ScriptManager.RegisterStartupScript(this, GetType(), "carga", script, true);

            }
            else
            {

                string script = "popupmsj.Show();";
                ScriptManager.RegisterStartupScript(this, GetType(), "carga", script, true);
            }

        }

        protected void btnSalir_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Repuestos/wFrmManualesPartesVehiculosSistemas.aspx?id=" + idNegocio);
        }

        protected void btnPopAceptar_Click(object sender, EventArgs e)
        {
            if (txtCodigo.Text == "" || txtDescripcion.Text == "" || txtDescripcionIngles.Text == "")
            {
                string script = "popupmsj.Show();";
                ScriptManager.RegisterStartupScript(this, GetType(), "carga", script, true);
            }
            else
            {
                T_MAN_ARTICULO objt_man_articulo = new T_MAN_ARTICULO();
                try
                {
                    objt_man_articulo.COD_MATERIAL = txtCodigo.Text;
                    objt_man_articulo.NOM_MATERIAL_ESP = txtDescripcion.Text;
                    objt_man_articulo.NOM_MATERIAL_INGLES = txtDescripcionIngles.Text;
                    objt_man_articulo.IND_ELIMINAR = 1;

                    BL_T_Man_SistemaArticulos.Modificar_Sistema_Articulos(objt_man_articulo);

                    listadoArticulos();

                }
                catch (Exception)
                {

                }

            }

        }

        protected void btnModificarArticuloClick(object sender, EventArgs e)
        {
            int seleccion = Convert.ToInt32(gvlistado.FocusedRowIndex.ToString());
            if (seleccion != -1)
            {
                string script = "pcModificarArticulo.Show();";
                ScriptManager.RegisterStartupScript(this, typeof(Page), "carga", script, true);
            }
            else
            {
                string script = "popupmsj.Show();";
                ScriptManager.RegisterStartupScript(this, typeof(Page), "carga", script, true);
            }
        }

       
    }
}